//
//  FMFirstViewController.m
//  PsychologyFM
//
//  Created by zhang on 16/1/11.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMRootViewController.h"
#import "FMHomeViewController.h"
#import "FMCommunityViewController.h"
#import "FMFindViewController.h"
#import "FMSettingViewController.h"
#import "FMUserViewController.h"
#import "FMRootViewController.h"

#import <FMDB.h>

@interface FMRootViewController ()<UIScrollViewDelegate,UITabBarDelegate>
@property (nonatomic, strong) FMSettingViewController *setVC;
@property (nonatomic, strong) FMUserViewController *userVC;
@property (nonatomic, strong) FMCommunityViewController *comuVC;
@property (nonatomic, strong) FMHomeViewController *homeVC;
@property (nonatomic, strong) FMFindViewController *findVC;
@property (nonatomic,strong) FMDatabase *db;



//创建5个导航控制器的属性
@property (nonatomic, strong) UINavigationController *commuNaVC;
@property (nonatomic, strong)  UINavigationController *homeNaVC;
@property (nonatomic, strong) UINavigationController *findNaVC;
@property (nonatomic, strong) UINavigationController *setNaVC;
@property (nonatomic, strong) UINavigationController *userNaVC;




@end

@implementation FMRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  创建我们的数据库的路径 同时拼接数据库的文件名称
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"FMDataBase.db"];
    //  根据路径去创建一个数据库
    _db = [FMDatabase databaseWithPath:path];
//    NSLog(@"%@",path);
    
    //  调用open方法 打开成功返回YES 失败 返回NO
    if ([_db open]) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
    
    //  调用db的executeUpdata 这个方法的参数就是SQL 语句
    [_db executeUpdate:@"CREATE TABLE UserInformationlist (isLogin text,avatar text, user_id text, introduce text, nickname text, qq_openid text, renren_openid text, sina_openid text, expire text, token text)"];
    
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
  //  self.navigationController.tabBarItem.


    [self addSubViews];
}

- (void)addSubViews{
    

#pragma mark --- 创建scrollview
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 )];
    _scrollView.tag = 100;
    
    
#pragma mark --- 首页
    
    self.homeVC = [[FMHomeViewController alloc]init];
    _homeVC.rootVC = self;
    _homeVC.view.backgroundColor = [UIColor whiteColor];
    _homeNaVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight );
    _homeNaVC = [[UINavigationController alloc]initWithRootViewController:_homeVC];
    
    
#pragma mark --- 发现
    self.findVC = [[FMFindViewController alloc]init];
    _findNaVC = [[UINavigationController alloc]initWithRootViewController:_findVC];
    _findNaVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 49);
    _findVC.rootVC = self;
    _findNaVC.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark --- 社区
    _comuVC = [[FMCommunityViewController alloc]init];
    _comuVC.rootVC = self;
    _commuNaVC = [[UINavigationController alloc]initWithRootViewController:_comuVC];
    
    _commuNaVC.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, kScreenHeight - 49);
    
    
#pragma mark --- 设置
    FMSettingViewController *setVC = [[FMSettingViewController alloc]init];
    _setNaVC = [[UINavigationController alloc]initWithRootViewController:setVC];
    _setNaVC.view.frame = CGRectMake(kScreenWidth * 4, 0, kScreenWidth, kScreenHeight - 49);
    
    setVC.view.backgroundColor = [UIColor whiteColor];
    
    
#pragma mark --- 我的
    self.userVC = [[FMUserViewController alloc]init];
    _userNaVC = [[UINavigationController alloc]initWithRootViewController:_userVC];
    
    _userNaVC.view.frame = CGRectMake(kScreenWidth * 3, 0, kScreenWidth, kScreenHeight - 49);
    _userVC.view.backgroundColor = [UIColor whiteColor];

    
    
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * 5, kScreenHeight - 49 - 64 );
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.alwaysBounceHorizontal = NO;
    self.scrollView.alwaysBounceVertical = NO;
    
    
    
    
#pragma mark --- 添加tabBar
    self.tabbar = [[UITabBar alloc]initWithFrame:CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49)];
    self.tabbar.tag = 6;
    self.tabbar.delegate = self;
    
    //首页
    UIImage *homeImageSelected = [UIImage imageNamed:@"home_selected"];
    UIImage *homeImage = [UIImage imageNamed:@"home_normal"];
    UITabBarItem *home = [[UITabBarItem alloc]initWithTitle:@"首页" image:homeImage selectedImage:homeImageSelected];
    home.tag = 1;
    
    //发现
    UIImage *findImage = [UIImage imageNamed:@"find_normal"];
    UIImage *findImageSelected = [UIImage imageNamed:@"find_selected"];
    UITabBarItem *find = [[UITabBarItem alloc]initWithTitle:@"发现" image:findImage selectedImage:findImageSelected];
    find.tag = 2;
    
    //社区
    UIImage *community_normal = [UIImage imageNamed:@"community_normal"];
    UIImage *community_selected = [UIImage imageNamed:@"community_selected"];
    UITabBarItem *community = [[UITabBarItem alloc]initWithTitle:@"社区" image:community_normal selectedImage:community_selected];
    community.tag = 3;
    
    
    //我的
    UIImage *user_normal = [UIImage imageNamed:@"user_normal"];
    UIImage *user_selected = [UIImage imageNamed:@"user_selected"];
    UITabBarItem *user = [[UITabBarItem alloc]initWithTitle:@"我的" image:user_normal selectedImage:user_selected];
    user.tag = 4;
    
    //设置
    UIImage *setting_normal = [UIImage imageNamed:@"setting_normal"];
    UIImage *setting_selected = [UIImage imageNamed:@"setting_selected"];
    UITabBarItem *set = [[UITabBarItem alloc]initWithTitle:@"设置" image:setting_normal selectedImage:setting_selected];
    set.tag = 5;
    
    
    
    _tabbar.items = @[home,find, community,user,set];
    
    _tabbar.selectedItem = home;
    
    

    
    [self.scrollView addSubview:_homeNaVC.view];
    [self.scrollView addSubview:_findNaVC.view];
    [self.scrollView addSubview:_commuNaVC.view];
    [self.scrollView addSubview:_userNaVC.view];
    [self.scrollView addSubview:_setNaVC.view];
    
    [self.view addSubview:self.scrollView];
    
    [self.view addSubview:_tabbar];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.num = scrollView.contentOffset.x / kScreenWidth;
    _tabbar.selectedItem = _tabbar.items[self.num];
    
    
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSInteger index = item.tag;
    self.scrollView.contentOffset = CGPointMake(kScreenWidth * (index - 1), 0);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
